//
//  CustomHouseCaseOnShowViewController.m
//  customer
//
//  Created by Holothurian on 2018/11/2.
//  Copyright © 2018年 yinlong. All rights reserved.
//

#import "CustomHouseCaseOnShowViewController.h"
#import "CasesShowModel.h"
#import "HSImageScrollViewController.h"
#import "CasesShowCollectionViewCell.h"
#import "CustomAddViewController.h"

static NSString *ItemIdentifier = @"ItemIdentifier";
@interface CustomHouseCaseOnShowViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>{
    UICollectionViewFlowLayout *_flowLayout;
    
}

@property (nonatomic,strong)UICollectionView *collectionView;

@property (nonatomic,strong)NSMutableArray *dataSource;


@end

@implementation CustomHouseCaseOnShowViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = [NSString stringWithFormat:@"%@效果图",self.caseTypeModel.programme_name];
    self.imgUrlArray = [NSMutableArray array];
    [self layoutCollectionView];
    [self loadDataSource];
}

- (void)loadDataSource{
    AutoWeakSelf
    [self showLoading];
    NSDictionary *dict = @{@"programme_id":self.caseTypeModel.programme_id?:@"-1"};
    [[QYNetManager sharedManager]POST:kGetProgrammeImgs parameters:dict andApiTyp:CustomerApiTypGetOrders SuccessBlock:^(id responseObject) {
        [weakSelf hideLoading];
        if ([responseObject[@"status"] isEqualToString:@"error"]) {
            [weakSelf showErrorTipWithTitle:responseObject[@"error"]];
        }else{
            NSArray *dataSource = responseObject[@"data"];
            [weakSelf.imgUrlArray addObjectsFromArray:[NSArray yy_modelArrayWithClass:[GetProgromImages class] json:dataSource]];
            for (GetProgromImages *model in weakSelf.imgUrlArray) {
                [weakSelf.dataSource addObject:model.programme_img];
            }
        }
        [weakSelf.collectionView reloadData];
    } Failure:^(NSError *error) {
        [weakSelf hideLoading];
        [weakSelf showErrorTipWithTitle:error.localizedDescription];
    }];
}


- (void)layoutCollectionView{
    _flowLayout = [[UICollectionViewFlowLayout alloc]init];
    _flowLayout.itemSize = CGSizeMake((F_DEVICE_W - 45) / 2.0, F_DEVICE_H / 4.0);
    _flowLayout.minimumLineSpacing = 20;
    _flowLayout.minimumInteritemSpacing = 15;
    _flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, NAVIGATION_BAR_HEIGHT, F_DEVICE_W, F_DEVICE_H - NAVIGATION_BAR_HEIGHT - bottomPadding(100)) collectionViewLayout: _flowLayout];
    collectionView.bounces = NO;
    collectionView.delegate = self;
    collectionView.dataSource = self;
    collectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:collectionView];
    self.collectionView = collectionView;
    [self.collectionView registerClass:[CasesShowCollectionViewCell class] forCellWithReuseIdentifier:ItemIdentifier];
    
    
    UIButton *appointButton = [UIButton buttonWithType:UIButtonTypeCustom];
    appointButton.frame = CGRectMake(30, F_DEVICE_H - NAVIGATION_BAR_HEIGHT - bottomPadding(0), F_DEVICE_W - 60, 50);
    appointButton.layer.cornerRadius = 6;
    appointButton.layer.masksToBounds = YES;
    [appointButton setBackgroundColor:[HSPublic colorWithHexString:@"333333"]];
    [appointButton setTitle:@"确定" forState:UIControlStateNormal];
    [appointButton setTitle:@"确定" forState:UIControlStateSelected];
    [appointButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [appointButton setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
    [appointButton addTarget:self action:@selector(appointmentAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:appointButton];
    
}

- (void)appointmentAction:(UIButton *)sender{
    CustomAddViewController *custom = [CustomAddViewController new];
    custom.caseListModel = self.caseListModel;
    custom.houseTypeModel = self.houseTypeModel;
    custom.caseTypeModel = self.caseTypeModel;
    TAB_PUSH(self, custom, YES);
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return [self.imgUrlArray count];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    CasesShowCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ItemIdentifier forIndexPath:indexPath];
    cell.titlelabel.hidden = YES;
    cell.effectView.hidden = YES;
    GetProgromImages *imageModel = self.imgUrlArray[indexPath.row];
    [cell.imgView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",kCustomerImageIP,imageModel.programme_img]] placeholderImage:[UIImage imageNamed:@"globalPlaceholder"]];
    return cell;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(10, 15, 10, 15);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    HSImageScrollViewController *scrollVC = [[HSImageScrollViewController alloc]init];
    scrollVC.imgUrlArr = self.dataSource;
    scrollVC.index = indexPath.row;
    [self presentViewController:scrollVC animated:NO completion:nil];
    
}

- (NSMutableArray *)dataSource{
    if(!_dataSource){
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
